package cn.com.gdt.microservice.auths.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import cn.com.gdt.microservice.auths.api.RMService;
import cn.com.gdt.microservice.auths.dao.RMMapper;
import net.sf.json.JSONObject;


public class RMServiceImpl implements RMService{
	@Autowired
	RMMapper rmMapper;
	@Autowired
	private RedisTemplate redisTemplate;
	@Override 
	public Map<String, Object> getUserBelongOrg(String userid) {
		return rmMapper.getOrgByUserId(userid);
	}

	@Override
	public Map<String, Object> getUserInfo(String userid) {
		Map<String,Object> result = new HashMap();
		Map<String,Object> map = rmMapper.getUserInfoByUserId(userid);
		result.put("userId", map.get("id"));
		result.put("userName", map.get("userName"));
		result.put("roleName", map.get("roleName"));
		if(map.get("other")!=null){
			Map m = JSONObject.fromObject(map.get("other").toString());
			//{"nikeName":"金属","age":"34","sex":"F","cardNo":"343434343333344444"}
			result.put("name", m.get("nikeName"));
			result.put("age", m.get("age"));
			result.put("sex", m.get("sex"));
			result.put("cardNo", m.get("cardNo"));
		}
		return result;
	}

	@Override
	public Map<String, Object> getOrgInfo(String orgid) {
		return rmMapper.getOrgInfoByOrgId(orgid);
	}

	@Override
	public List<Map<String, Object>> getUserInfoList() {
		List<Map<String,Object>> list = rmMapper.getUserInfoList();
		for(Map map:list){
			if(map.get("other")!=null){
				Map m = JSONObject.fromObject(map.get("other").toString());
				map.put("name", m.get("nikeName"));
				map.put("age", m.get("age"));
				map.put("sex", m.get("sex"));
				map.put("cardNo", m.get("cardNo"));
			}
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getOrgInfoList() {
		return rmMapper.getOrgInfoList();
	}
	public List<Map<String,Object>> getDictType(){
		Map map = new HashMap();
		List<Map<String,Object>> list = rmMapper.getDictType();
		for(Map m:list){
			if(map.containsKey(m.get("ecode"))){
				Map _ms = (Map) map.get(m.get("ecode"));
				List _items = (List) _ms.get("items");
				Map _item = new HashMap();
				_item.put("ekey", m.get("ekey"));
				_item.put("eval", m.get("eval"));
				_items.add(_item);
				_ms.put("items", _items);
				map.put(m.get("ecode"), _ms);
			}else{
				Map ms = new HashMap();
				List items = new ArrayList();
				Map item = new HashMap();
				ms.put("ename", m.get("ename"));
				item.put("ekey",m.get("ekey"));
				item.put("eval", m.get("eval"));
				items.add(item);
				ms.put("items", items);
				map.put(m.get("ecode"), ms);
			}
		}
		redisTemplate.opsForHash().putAll("dictType", map);
		Map res = (Map) redisTemplate.opsForHash().get("dictType","UNIT");
		List<Map<String,Object>> l = (List<Map<String,Object>>) res.get("items");
		return l;
	}
}
